package com.duoketech.service.web.room;
import com.duoketech.config.Constant;
import com.duoketech.model.activity.Activity;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.model.member.Member;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.service.AbstractService;
import com.duoketech.model.room.UnitRoom;
import com.duoketech.exception.ValidateException;
import com.duoketech.service.weixin.room.WeiXinRoomService;
import com.duoketech.utils.DateUtils;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.WxMsgTemplateUtils;
import com.duoketech.utils.search.EqualCondition;
import com.duoketech.utils.search.LikeCondition;
import com.duoketech.utils.search.NotEqualCondition;
import com.duoketech.utils.search.QueryConstructor;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeMobile;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

public class UnitRoomService extends AbstractService{
    public static UnitRoomService ME = new UnitRoomService();

    @Override
    public void validatorUpdate(Model<?> model, Kv kv) throws ValidateException {
        validatorAdd(model,kv);
    }

    @Override
    public void validatorAdd(Model<?> model, Kv kv) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(model.getStr(UnitRoom.NAME),"房间名称项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(UnitRoom.NAME),"房间名称项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(UnitRoom.DESC),"房间简介项为必填信息"));
//        validator.addRule(new StringLengthBetween(model.getStr(UnitRoom.DESC),"房间简介项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(UnitRoom.PRICE),"房价项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(UnitRoom.PRICE),"房价项字符必须为1-50个字符",1,50));
//        validator.addRule(new StringMustBeRequired(model.getStr(UnitRoom.CREATE_BY),"创建者项为必填信息"));
//        validator.addRule(new StringLengthBetween(model.getStr(UnitRoom.CREATE_BY),"创建者项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(UnitRoom.STATUS),"房间状态项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(UnitRoom.STATUS),"房间状态项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(UnitRoom.EQUIP),"配套设施项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(UnitRoom.EQUIP),"配套设施项字符必须为1-50个字符",1,50));
        validator.excute();
    }

    @Override
    public void beforAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public String tableName() {
        return UnitRoom.dao.tableName();
    }

    @Override
    public void builderSQLExpected(Model<?> model, QueryConstructor queryConstruct, Kv kv) {

        queryConstruct.and(new NotEqualCondition(UnitRoom.STATUS,UnitRoom.STATUS_DEL));
        queryConstruct.and(new EqualCondition(UnitRoom.CREATE_BY,kv.getInt(Constant.MEMBER_ID_KEY)));
        String value = model.getStr(UnitRoom.NAME);
        if(StringUtils.isNotEmpty(value)){
            queryConstruct.and(new LikeCondition(UnitRoom.NAME,"%"+value+"%"));
        }

    }


    public int doAddRoomOrder( RoomOrder roomOrder) throws ValidateException{
        Validator validator =new Validator();

        String name = roomOrder.getStr(RoomOrder.NAME);
        String mobile = roomOrder.getStr(RoomOrder.MOBILE);
        int cardType = roomOrder.getInt(RoomOrder.CARD_TYPE);
        String cardNo = roomOrder.getStr(RoomOrder.CARD_NO);
        Date startTime = roomOrder.getDate(RoomOrder.START_TIME);
        Date endTime = roomOrder.getDate(RoomOrder.END_TIME);

        validator.addRule(new StringMustBeRequired(name,"请输入联系人姓名"));
        validator.addRule(new StringMustBeRequired(mobile,"请输入联系人电话"));
        validator.addRule(new StringMustBeMobile(mobile,"请输入正确的联系人电话"));
        validator.addRule(new StringMustBeRequired(cardType+"","请选择证件类型"));
        validator.addRule(new StringMustBeRequired(cardNo,"请输入证件号码"));
        validator.addRule(new StringMustBeRequired(PageUtil.formatDataYYMMDD(startTime),"请输入入住时间"));
        validator.addRule(new StringMustBeRequired(PageUtil.formatDataYYMMDD(endTime),"请输入离店时间"));
        validator.excute();
        if(endTime.before(startTime)){
            throw  new ValidateException("离店时间必须在入驻时间之前");
        }
        int objId = roomOrder.getInt(RoomOrder.OBJ_ID);
//        RoomOrder temp = WeiXinRoomService.ME.queryRoomByCardNo(cardNo,objId);
//        if(temp!=null){
//            throw  new ValidateException("您已经预订了当前客栈了，请勿重复预订");
//        }
        int id = roomOrder.getInt(RoomOrder.OBJ_ID);
        UnitRoom room = UnitRoom.dao.findById(id);
        if(room!=null){
//            roomOrder.set(RoomOrder.NAME,"胡称颂");
//            roomOrder.set(RoomOrder.MOBILE,"13741489217");
//            roomOrder.set(RoomOrder.START_TIME,"2018-09-01");
//            roomOrder.set(RoomOrder.END_TIME,"2018-09-11");
//            roomOrder.set(RoomOrder.REMARKER,"我需要接机服务");
            roomOrder.set(RoomOrder.OBJ_ID,room.getInt(UnitRoom.ID));
            roomOrder.set(RoomOrder.PRICE,room.getInt(UnitRoom.PRICE)*1);
            roomOrder.set(RoomOrder.STATUS,RoomOrder.STATUS_NORMAL);
            roomOrder.set(RoomOrder.CREATE_TIME,new Date());
            roomOrder.set(RoomOrder.UNIT_ID,room.getInt(UnitRoom.CREATE_BY));
            roomOrder.set(RoomOrder.ORDER_SN,System.currentTimeMillis()+"");
            roomOrder.save();


            //向客栈预订者发送消息提醒
            int memberId = roomOrder.getInt(ActivityOrder.MEMBER_ID);
            Member member = Member.dao.findById(memberId);
            WxMsgTemplateUtils.sendRoomCheckInMsg(member.getStr(Member.OPEN_ID),member.getStr(Member.NICK_NAME),
                    room.getStr(UnitRoom.NAME),roomOrder.startTime(),roomOrder.endTime());

            //向客栈管理者发送消息提醒
            member = Member.dao.findById(room.getInt(UnitRoom.CREATE_BY));

            WxMsgTemplateUtils.sendRoomCheckInToOwnerMsg(member.getStr(Member.OPEN_ID),member.getStr(Member.NICK_NAME),
                    room.getStr(UnitRoom.NAME),roomOrder.startTime(),roomOrder.endTime()
            ,roomOrder.getStr(RoomOrder.NAME),roomOrder.getStr(RoomOrder.MOBILE));

        }
        return roomOrder.getInt(RoomOrder.ID);
    }

    public void cancelRoomOrder(int id,int memberId ) throws ValidateException {
        RoomOrder roomOrder = RoomOrder.dao.findById(id);
        if(roomOrder==null){
            throw  new ValidateException("当前记录不存在");
        }

        if(roomOrder.getInt(RoomOrder.MEMBER_ID)!=memberId){
            throw  new ValidateException("非本人预订的房间，无权限取消");
        }

        if(roomOrder.getInt(RoomOrder.STATUS)==RoomOrder.STATUS_CANCEL){
            throw  new ValidateException("当前预订的房间已经被取消预订了，无法再进行操作");
        }
        roomOrder.set(RoomOrder.STATUS,RoomOrder.STATUS_CANCEL);
        roomOrder.set(RoomOrder.MODIFY_TIME,new Date());
        roomOrder.update();




        UnitRoom unitRoom = UnitRoom.dao.findById(roomOrder.getInt(RoomOrder.OBJ_ID));

        //向预订人发送消息取消订单提醒
        Member member  = Member.dao.findById(roomOrder.getInt(RoomOrder.MEMBER_ID));
        WxMsgTemplateUtils.sendOrderCancelMsg(member.getStr(Member.OPEN_ID),
                member.getStr(Member.NICK_NAME),"客栈房间-"+unitRoom.getStr(UnitRoom.NAME),
                "ROOM"+member.getInt(Member.ID)+"-"+roomOrder.getInt(RoomOrder.ID));

        //向发起人发送消息取消订单提醒
        member  = Member.dao.findById(unitRoom.getInt(UnitRoom.CREATE_BY));
        WxMsgTemplateUtils.sendOrderCancelToOwnerMsg(member.getStr(Member.OPEN_ID),
                member.getStr(Member.NICK_NAME),"客栈房间-"+unitRoom.getStr(UnitRoom.NAME),
                "ROOM"+member.getInt(Member.ID)+"-"+roomOrder.getInt(RoomOrder.ID),
                roomOrder.getStr(RoomOrder.NAME),roomOrder.getStr(RoomOrder.MOBILE));
    }


    public UnitRoom queryById(int id ){
        return UnitRoom.dao.findById(id);
    }
}